<template>
  <div class="main">
    <div class="air-column">
      <h2>乘机人</h2>
      <el-form class="member-info">
        <div
          class="member-info-item"
          v-for="(user, index) in users"
          :key="user.uid"
        >
          <el-form-item label="乘机人姓名">
            <el-input
              v-model="user.username"
              placeholder="姓名"
              class="input-with-select"
            >
            </el-input>
          </el-form-item>

          <el-form-item label="身份证号码">
            <el-input
              v-model="user.id"
              placeholder="证件号码"
              class="input-with-select"
            >
            </el-input>
          </el-form-item>

          <span class="delete-user" @click="handleDeleteUser(index)">-</span>
        </div>
      </el-form>

      <el-button class="add-member" type="primary" @click="handleAddUsers"
        >添加乘机人</el-button
      >
    </div>

    <div class="air-column">
      <h2>保险</h2>
      <div>
        <el-checkbox-group v-model="insurances">
          <div
            class="insurance-item"
            v-for="item in flightsData.insurances"
            :key="item.id"
          >
            <el-checkbox :label="item.id" border>
              {{
                `${item.type}: ￥${item.price}/份 x ${users.length} 最高赔付 ${item.compensation}`
              }}
            </el-checkbox>
          </div>
        </el-checkbox-group>
      </div>
      <h2>发票</h2>
      <div>
        <el-checkbox v-model="invoice">需要发票</el-checkbox>
      </div>
    </div>

    <div class="air-column">
      <h2>联系人</h2>
      <div class="contact">
        <el-form label-width="60px">
          <el-form-item label="姓名">
            <el-input v-model="contactName"></el-input>
          </el-form-item>

          <el-form-item label="手机">
            <el-input placeholder="请输入内容" v-model="contactPhone">
              <template slot="append">
                <el-button @click="handleSendCaptcha">发送验证码</el-button>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item label="验证码">
            <el-input v-model="captcha"></el-input>
          </el-form-item>
        </el-form>
        <el-button type="warning" class="submit" @click="handleSubmit"
          >提交订单</el-button
        >
      </div>
    </div>
    <!-- <div style="display: none">
      {{ totalPrice }}
    </div> -->
  </div>
</template>

<script>
export default {
  props: {
    flightsData: Object,
  },
  data() {
    return {
      users: [
        {
          // 用户名
          username: "哈哈",
          // 身份证号
          id: "450433222222222222",
          uid: new Date().getTime(),
        },
      ],
      insurances: [],
      contactName: "嘿嘿",
      contactPhone: "13847474837",
      captcha: "000000",
      invoice: false,
    };
  },

  watch: {
    users: {
      handler() {
        this.totalPrice();
      },
      immediate: true,
    },
    insurances: {
      handler() {
        this.totalPrice();
      },
      immediate: true,
    },
  },
  methods: {
    totalPrice() {
      console.log(this.flightsData, "flightsData");
      const ticketPrice = this.flightsData.base_price * this.users.length;
      let insurancePrice = 0;
      this.insurances.forEach((element) => {
        // 拿到了id以后应该根据这个id获取保险的价格
        this.data.insurances.forEach((insurance) => {
          if (insurance.id == element) {
            // 拿到了当前选中 id 保险的价格,
            // 应该乘以人数, 添加到总价上
            insurancePrice += insurance.price * this.users.length;
          }
        });
      });
      console.log(insurancePrice);
      const totalPrice = ticketPrice + insurancePrice;
      // 得出了总价
      this.$emit("sentFlightAmount", totalPrice);
      console.log("计算总价" + totalPrice);
      // return totalPrice;
    },

    handleSubmit() {
      console.log("点击按钮提交");
      // console.log(this.users);
      // console.log(this.insurances);
      // 开始拼接数据
      const data = {
        users: this.users,
        insurances: this.insurances,
        contactName: this.contactName,
        contactPhone: this.contactPhone,
        invoice: this.invoice,
        seat_xid: this.$route.query.seat_xid,
        air: this.$route.query.id,
        captcha: this.captcha,
      };

      this.$axios({
        method: "post",
        url: "/airorders",
        headers: {
          Authorization: "Bearer " + this.$store.state.user.userInfo.token,
        },
        data,
      }).then((res) => {
        console.log(res.data);
      });
    },
    handleSendCaptcha() {
      if (!this.contactPhone) {
        this.$message.error("请先输入手机号");
        return;
      }
      this.$axios({
        method: "post",
        url: "/captchas",
        data: {
          tel: this.contactPhone,
        },
      }).then((res) => {
        console.log(res.data);
        if (res.data.code) {
          this.$message.success("发送成功：" + res.data.code);
        }
      });
    },
    handleAddUsers() {
      this.users.push({
        // 用户名
        username: "",
        // 身份证号
        id: "",
        uid: new Date().getTime(),
      });
    },
    handleDeleteUser(index) {
      this.users.splice(index, 1);
    },
  },
};
</script>


<style scoped lang="less">
.air-column {
  border-bottom: 1px #ddd dashed;
  padding-bottom: 20px;
  margin-bottom: 20px;
}

.air-column h2 {
  margin-bottom: 20px;
  font-size: 22px;
  font-weight: normal;
}

/deep/ .el-select .el-input {
  width: 130px;
}

.input-with-select {
  width: 590px;
}

.input-with-select /deep/ .el-input-group__prepend {
  background-color: #fff;
}
.member-info /deep/ .el-form-item {
  margin-bottom: 0;
}

.member-info-item {
  border-bottom: 1px #eee dashed;
  padding-bottom: 20px;
  position: relative;

  &:first-child {
    .delete-user {
      display: none;
    }
  }
}

.add-member {
  margin-top: 20px;
}

.delete-user {
  display: block;
  background: #ddd;
  width: 16px;
  height: 16px;
  font-size: 14px;
  text-align: center;
  line-height: 16px;
  color: #fff;
  cursor: pointer;
  border-radius: 50px;
  position: absolute;
  right: -30px;
  top: 50%;
}

.insurance {
  > div {
    margin-top: 10px;
  }
}

.insurance-item {
  margin-bottom: 20px;
}

.contact {
  /deep/ .el-input {
    width: 50%;
  }
}

.submit {
  margin: 50px auto;
  display: block;
  width: 250px;
  height: 50px;
}
</style>